package com.lynbermt.lodge;

import java.text.DecimalFormat;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class FriendFinderActivity extends MapActivity{

	private MapController mapController;
	private LocationManager locationManager;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.maplayout);
        
        MapView myMapView = (MapView)findViewById(R.id.mapview);
        mapController = myMapView.getController();
        myMapView.setSatellite(false);
        myMapView.displayZoomControls(true);
        mapController.setZoom(14);
        
        String context = Context.LOCATION_SERVICE;
        locationManager = (LocationManager)getSystemService(context);
        
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        
        String provider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(provider);
        
        updateWithNewLocation(location);
        
        locationManager.requestLocationUpdates(provider,0,0,locationListener);
    }//end onCreate

	private void updateWithNewLocation(Location location){
		String latLongString;
		TextView myLocationText = (TextView)findViewById(R.id.myLocationText);
		
		if(location!=null){
			double geoLat = location.getLatitude()*1E6;
			double geoLng = location.getLongitude()*1E6;
			DecimalFormat logLatFormat = new DecimalFormat("##.######");
			latLongString = "Lat: " + logLatFormat.format(geoLat) + "\nLong: " + logLatFormat.format(geoLng);
			GeoPoint point = new GeoPoint((int)geoLat,(int)geoLng);
			mapController.animateTo(point);

		}else{
			latLongString = "Location\nUnknown";
		}
	myLocationText.setText("Your Current Position is:\n" + latLongString);	
	}//end updateWithNewLocation

    private final LocationListener locationListener = new LocationListener(){

		@Override
		public void onLocationChanged(Location location) {
			updateWithNewLocation(location);				
		}
		@Override
		public void onProviderDisabled(String provider) {
			updateWithNewLocation(null);	
		}
		@Override
		public void onProviderEnabled(String provider) {}

		@Override
		public void onStatusChanged(String provider, int status,
				Bundle extras){}
    };

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	
	@Override
	protected void onPause() {
		super.onPause();
		if(locationManager != null){
			locationManager.removeUpdates(locationListener);
		}
	}//end onPause
	
	@Override
	protected void onResume(){
		super.onResume();     
		
		Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(true);
        criteria.setBearingRequired(true);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        
        String provider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(provider);
		updateWithNewLocation(location);
	}

}
